﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;
using YiSha.Service.QixingManage;

namespace YiSha.Business.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2023-04-09 19:28
    /// 描 述：优惠劵业务类
    /// </summary>
    public class CouponBLL
    {
        private CouponService couponService = new CouponService();
        private CouponShopRelationService couponShopRelationService = new CouponShopRelationService();
        private CouponGoodsRelationService couponGoodsRelationService = new CouponGoodsRelationService();
        private ShopService shopService = new ShopService();
        private GoodsService goodsService = new GoodsService();
        private MemberCouponService memberCouponService = new MemberCouponService();

        #region 获取数据
        public async Task<TData<List<CouponEntity>>> GetList(CouponListParam param)
        {
            TData<List<CouponEntity>> obj = new TData<List<CouponEntity>>();
            obj.Data = await couponService.GetList(param);
            obj.Total = obj.Data.Count;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<List<CouponEntity>>> GetPageList(CouponListParam param, Pagination pagination)
        {
            TData<List<CouponEntity>> obj = new TData<List<CouponEntity>>();
            obj.Data = await couponService.GetPageList(param, pagination);
            obj.Total = pagination.TotalCount;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<CouponEntity>> GetEntity(long id)
        {
            TData<CouponEntity> obj = new TData<CouponEntity>();
            obj.Data = await couponService.GetEntity(id);
            //判断类型
            switch (obj.Data.use_type)
            {
                case 0:
                    break;
                case 1://分类暂时未开发
                    break;
                case 2:
                    CouponShopRelationListParam ShoplistParam = new CouponShopRelationListParam();
                    ShoplistParam.couponid = obj.Data.Id.Value;
                    string shopids=string.Join(',',couponShopRelationService.GetList(ShoplistParam).Result.Select(t=>t.shop_id));
                    obj.Data.shopids=shopids;
                    break;
                case 3:
                    CouponGoodsRelationListParam GoodslistParam = new CouponGoodsRelationListParam();
                    GoodslistParam.couponid = obj.Data.Id.Value;
                    string goodsids = string.Join(',', couponGoodsRelationService.GetList(GoodslistParam).Result.Select(t => t.goods_id));
                    obj.Data.goodsids = goodsids;
                    break;
                default:
                    break;
            }

            if (obj.Data != null)
            {
                obj.Tag = 1;
            }
            return obj;
        }
        #endregion

        #region 提交数据
        public async Task<TData<string>> SaveForm(CouponEntity entity)
        {
            TData<string> obj = new TData<string>();
            await couponService.SaveForm(entity);

            //判断优惠劵使用类型
            switch (entity.use_type)
            {
                case 0:
                    break;
                case 1://分类暂时未开发
                    break;
                case 2:
                    await couponGoodsRelationService.DeleteForm(entity.Id.Value);
                    CouponBindShop(entity);
                    break;
                case 3:
                    await couponShopRelationService.DeleteForm(entity.Id.Value);
                    CouponBindGoods(entity);
                    break;
                default:
                    break;
            }

            obj.Data = entity.Id.ParseToString();
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData> DeleteForm(string ids)
        {
            TData obj = new TData();

            long[] idArr = TextHelper.SplitToArray<long>(ids, ',');

            var delarr=couponService.GetList(t => idArr.Contains(t.Id.Value)).Result;

            await couponService.DeleteForm(ids);

            //撤销掉领取的优惠劵和优惠劵关系表
            foreach (var item in delarr)
            {
                switch (item.use_type)
                {
                    case 0:
                        break;
                    case 1://分类暂时未开发
                        break;
                    case 2:
                      await  couponGoodsRelationService.DeleteForm(item.Id.Value);
                        break;
                    case 3:
                        await couponShopRelationService.DeleteForm(item.Id.Value);

                        break;
                    default:
                        break;
                }
                await memberCouponService.DeleteForm(t => t.coupon_id == item.Id);
            }

            obj.Tag = 1;
            return obj;
        }
        #endregion

        #region 私有方法
        /// <summary>
        /// 绑定商铺
        /// </summary>
        public void CouponBindShop(CouponEntity entity)
        {
            var shopIdArr=TextHelper.SplitToArray<long>(entity.shopids, ',');
            foreach (var item in shopIdArr)
            {
                //判断数据是否存在，存在就跳过
                CouponShopRelationEntity  couponShop=couponShopRelationService.GetEntity(t => t.shop_id == item && t.coupon_id == entity.Id).Result;
                if (couponShop.IsEmpty())
                {
                    var shopinfo = shopService.GetEntity(item).Result;
                    CouponShopRelationEntity shopRelationEntity = new CouponShopRelationEntity();
                    shopRelationEntity.coupon_id = entity.Id;
                    shopRelationEntity.shop_id = item;
                    shopRelationEntity.shop_name = shopinfo.shop_name;
                    couponShopRelationService.SaveForm(shopRelationEntity);
                }
            }
        }
        /// <summary>
        /// 绑定商品
        /// </summary>
        public void CouponBindGoods(CouponEntity entity)
        {
            var goodsIdArr = TextHelper.SplitToArray<long>(entity.goodsids, ',');
            foreach (var item in goodsIdArr)
            {
                //判断数据是否存在，存在就跳过
                CouponGoodsRelationEntity couponGoods= couponGoodsRelationService.GetEntity(t => t.goods_id == item && t.coupon_id == entity.Id).Result;
                if (couponGoods.IsEmpty())
                {
                    var goodsinfo = goodsService.GetEntity(item).Result;
                    CouponGoodsRelationEntity goodsRelationEntity = new CouponGoodsRelationEntity();
                    goodsRelationEntity.coupon_id = entity.Id;
                    goodsRelationEntity.goods_id = item;
                    goodsRelationEntity.goods_name = goodsinfo.shop_name;
                    couponGoodsRelationService.SaveForm(goodsRelationEntity);
                }
            }
        }
        #endregion
    }
}
